package com.example.treehole.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.treehole.utils.StatusBarUtil;

import java.io.Serializable;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;
    public Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setColorNoTranslucent(this,Color.WHITE);
        setContentView(initLayout());
        mContext=this;
        unbinder= ButterKnife.bind(this);
        initData();
    }

    protected abstract int initLayout();

    protected abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    public void showToast(String msg){
        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
    }

    public void showToastSync(String msg){
        Looper.prepare();
        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
        Looper.loop();
    }


    public void jumpTo(Class cls){
        Intent intent=new Intent(mContext,cls);
        startActivity(intent);
    }

    public <T> void jumpTo(Class cls, T data){
        Intent intent=new Intent(mContext,cls);
        Bundle bundle=new Bundle();
        bundle.putSerializable("data", (Serializable) data);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //专门传图片的
    public <T> void jumpTo(String type,List<T> data,int position){
        Intent intent=new Intent(mContext,PhotoActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("type",type);
        bundle.putSerializable("data", (Serializable) data);
        bundle.putInt("position",position);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    protected void saveStringToSp(String key,String value){
        SharedPreferences sp = getSharedPreferences("treeHole",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key,value);
        editor.apply();
    }

    protected String getStringFromSp(String key){
        SharedPreferences sp=getSharedPreferences("treeHole",MODE_PRIVATE);
        return sp.getString(key,"");
    }

    protected void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

}
